﻿/*** MikeWare Framework ************************************
* This class is part of product of MikeWare.Framework.
* 
* Author      : Mike Cheers
* Mail        : mikecheers@126.com
* Create Date : 2018/9/16
* Summary     : 
* 
* 
* Modified By : 
* Date        : 
* Mail        : 
* Comment     : 
**************************************************************/

namespace MikeWare.Components.MikeLoggor.Policies
{
    using System;
    using MikeWare.Components.MikeLoggor.Listeners;

    public class FileSizeTriggeringPolicy : RollingPolicy
    {
        public Int64 FileSizeLimit = 5 * 1024 * 1024;

        public FileSizeTriggeringPolicy(Settings setting)
            : base(setting)
        {
            if (Int64.TryParse(Setting.Value, out FileSizeLimit))
                FileSizeLimit *= 1024 * 1024; ;
        }

        public override bool ShouldRolling(Listener listener)
        {
            if (listener is FileWriterListener)
            {
                var file = (listener as FileWriterListener).CurrentLogFile;
                file.Refresh();
                return file.Length >= FileSizeLimit;
            }

            return false;
        }
    }
}
